package com.ydl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydl.common.util.ResultVo;
import com.ydl.dao.GoodsDao;
import com.ydl.dao.UserDao;
import com.ydl.entry.pojo.Goods;
import com.ydl.entry.pojo.User;
import com.ydl.entry.vo.goods.AddGoodsParam;
import com.ydl.entry.vo.goods.DeleteGoodsParam;
import com.ydl.entry.vo.goods.QueryGoodsParam;
import com.ydl.entry.vo.goods.UpdateGoodsParam;
import com.ydl.entry.vo.user.LoginParam;
import com.ydl.entry.vo.user.LoginResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GoodsService {

    private final GoodsDao goodsDao;


    public ResultVo list(QueryGoodsParam queryGoodsParam) {
        List<Goods> goods = goodsDao.selectList(new LambdaQueryWrapper<Goods>()
                .like(queryGoodsParam.getGoodsName() != null, Goods::getGoodsName, queryGoodsParam.getGoodsName())
                .eq(queryGoodsParam.getGoodsPrice() != null, Goods::getGoodsPrice, queryGoodsParam.getGoodsPrice())
        );
        return ResultVo.success("查询成功", goods);
    }

    public ResultVo add(AddGoodsParam addGoodsParam) {
        Goods goods = new Goods();

        BeanUtils.copyProperties(addGoodsParam,goods);

        int insert = goodsDao.insert(goods);
        return ResultVo.success();
    }

    public ResultVo del(DeleteGoodsParam deleteGoodsParam) {
        int i = goodsDao.deleteById(deleteGoodsParam.getGoodsId());
        return ResultVo.success();
    }

    public ResultVo update(UpdateGoodsParam updateGoodsParam) {
        Goods goods = new Goods();

        BeanUtils.copyProperties(updateGoodsParam, goods);

        int i = goodsDao.updateById(goods);

        return ResultVo.success();
    }
}
